package com.shunda.ysdgj.manager.controller.system;

import com.shunda.ysdgj.manager.service.system.SysRoleService;
import com.shunda.ysdgj.manager.service.system.SysUserRoleService;
import com.shunda.ysdgj.model.dto.base.IdsDto;
import com.shunda.ysdgj.model.dto.system.SysRoleFindDto;
import com.shunda.ysdgj.model.entity.system.SysRole;
import com.shunda.ysdgj.model.entity.system.SysUserRole;
import com.shunda.ysdgj.model.vo.ResultVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Tag(name = "系统角色接口")
@RestController
@RequestMapping("/api/v1/admin/system/role")
@RequiredArgsConstructor
public class SysRoleController {

    private final SysRoleService sysRoleService;
    private final SysUserRoleService sysUserRoleService;

    @Operation(summary = "分配角色")
    @PostMapping("assign")
    public ResultVo assign(@RequestBody List<SysUserRole> sysUserRoleList) {
        if (!sysUserRoleService.add(sysUserRoleList)) {
            return ResultVo.fail();
        }
        return ResultVo.success();
    }

    @Operation(summary = "获取所有角色和已分配角色ID")
    @GetMapping("assigned/{userId}")
    public ResultVo assigned(@PathVariable Long userId) {
        Map<String, Object> responseData = new HashMap<>();
        responseData.put("roles", sysRoleService.findAll());
        responseData.put("assignedRoles", sysUserRoleService.getAssignedRoleIds(userId));
        return ResultVo.success(responseData);
    }

    @Operation(summary = "分页获取系统角色列表")
    @PostMapping("list")
    public ResultVo list(@RequestBody SysRoleFindDto sysRoleFindDto) {
        return ResultVo.success(sysRoleService.findByPage(sysRoleFindDto));
    }

    @Operation(summary = "添加系统角色")
    @PostMapping("add")
    public ResultVo add(@RequestBody SysRole sysRole) {
        if (!sysRoleService.add(sysRole)) {
            return ResultVo.fail();
        }
        return ResultVo.success();
    }

    @Operation(summary = "修改系统角色")
    @PostMapping("modify")
    public ResultVo modify(@RequestBody SysRole sysRole) {
        if (!sysRoleService.modify(sysRole)) {
            return ResultVo.fail();
        }
        return ResultVo.success();
    }

    @Operation(summary = "删除系统角色")
    @PostMapping("delete")
    public ResultVo delete(@RequestBody IdsDto idsDto) {
        if (!sysRoleService.deleteByIds(idsDto)) {
            return ResultVo.fail();
        }
        return ResultVo.success();
    }

}
